<template>
    <div class="app-container">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item prop="fWarmBoxCode">
          <el-input
            v-model="queryParams.fWarmBoxCode"
            placeholder="请输入温箱编码"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="添加日期">
              <el-date-picker
                v-model="queryParams.fCreateDate"
                style="width: 240px"
                value-format="yyyy-MM-dd"
                type="daterange"
                range-separator="-"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
              ></el-date-picker>
            </el-form-item>
        <!-- <el-form-item label="岗位名称" prop="postName">
          <el-input
            v-model="queryParams.postName"
            placeholder="请输入岗位名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item> -->
        <!-- <el-form-item label="状态" prop="status">
          <el-select v-model="queryParams.status" placeholder="岗位状态" clearable>
            <el-option
              v-for="dict in dict.type.sys_normal_disable"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item> -->
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
           <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"  v-hasPermi="['system:post:add']" >新增客户</el-button>
        </el-form-item>
      </el-form>
      <el-table v-loading="loading" :data="boxList" @selection-change="handleSelectionChange">
        <el-table-column label="序号" align="center" prop="fId" />
        <el-table-column label="温箱编码" align="center" prop="fWarmBoxCode" />
        <el-table-column label="温箱名称" align="center" prop="fWarmBoxName" />
        <el-table-column label="温箱识别编码" align="center" prop="fWarmBoxCode" />
        <el-table-column label="所属工厂" align="center" prop="fOrgId" />
        <el-table-column label="所属产线" align="center" prop="fLineId" />
        <el-table-column label="所属设备" align="center" prop="fDeviceBrand" width="180"/> 
        <el-table-column label="厂家名称" align="center" prop="fSupplierName" width="180"/> 
        <el-table-column label="品牌名称" align="center" prop="fDeviceBrand" width="180"/> 
        <el-table-column label="型号" align="center" prop="fDeviceModel" width="180"/> 
        <el-table-column label="最高温度(℃)" align="center" prop="fMaxNum" width="180"/> 
        <el-table-column label="最低温度(℃)" align="center" prop="fMinNum" width="180"/> 
        <el-table-column label="状态" align="center" prop="fState" width="180"/> 
        <el-table-column label="创建人" align="center" prop="fCreaterName" width="180"/> 
        <el-table-column label="创建日期" align="center" prop="fCreateDate" width="180"/> 
        <el-table-column label="最后修改人" align="center" prop="fModifierName" width="180"/> 
        <el-table-column label="最后修改日期" align="center" prop="fModifyDate" width="180"/> 
        <el-table-column fixed="right" label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button 
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['system:post:edit']"
            >查看</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['system:post:edit']"
            >编辑</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['system:post:remove']"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
  
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
  
      <!-- 添加或修改岗位对话框 -->
      <el-dialog :title="title" :visible.sync="open" append-to-body  width="85%">
        <el-form ref="form" :model="form" :rules="rules" label-width="130px">
          <el-col :span="8">
            <el-form-item label="所属工厂" required>
            <el-select v-model="form.fOrgId" placeholder="请选择活动区域">
            <el-option label="区域一" value="shanghai"></el-option>
            <el-option label="区域二" value="beijing"></el-option>
            </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="所属产线" required>
            <el-select v-model="form.fLineId" placeholder="请选择活动区域">
            <el-option label="区域一" value="shanghai"></el-option>
            <el-option label="区域二" value="beijing"></el-option>
            </el-select>
            </el-form-item>
          </el-col>  
          <el-col :span="8">
            <el-form-item label="所属设备" required>
            <el-select v-model="form.fDeviceBrand" placeholder="请选择活动区域">
            <el-option label="区域一" value="shanghai"></el-option>
            <el-option label="区域二" value="beijing"></el-option>
            </el-select>
            </el-form-item>
          </el-col>            
          <el-col :span="8">
            <el-form-item label="温箱名称" required>
              <el-input v-model="form.fWarmBoxName"></el-input>
            </el-form-item>
            <el-form-item label="型号">
              <el-input v-model="form.fDeviceModel"></el-input>
            </el-form-item>
            <el-form-item label="插槽数量">
              <el-input v-model="form.fSlotNum"></el-input>
            </el-form-item>
          </el-col> 
          <el-col :span="8">
            <el-form-item label="厂家名称">
              <el-input v-model="form.fSupplierName"></el-input>
            </el-form-item>
            <el-form-item label="最高温度(°C)">
              <el-input v-model="form.fMaxNum"></el-input>
            </el-form-item>
            <el-form-item label="温箱识别编号" required>
              <el-input v-model="form.fWarmBoxCode"></el-input>
            </el-form-item>
          </el-col> 
          <el-col :span="8">
            <el-form-item label="品牌名称">
              <el-input v-model="form.fDeviceBrand"></el-input>
            </el-form-item>
            <el-form-item label="最低温度(℃)">
              <el-input v-model="form.fMinNum"></el-input>
            </el-form-item>
            </el-col> 
            <el-col :span="8">
              <el-form-item label="是否启用">
                 <el-switch v-model="form.fState" active-color="#13ce66" inactive-color="#ff4949"
                  active-text="开启"
                  inactive-text="禁用"
                 > </el-switch>
              </el-form-item>
            </el-col> 
            <el-col :span="12">
              <el-form-item label="产品图片">
              <el-upload
                class="avatar-uploader"
                action="https://jsonplaceholder.typicode.com/posts/"
                :show-file-list="false"
                :on-success="handleAvatarSuccess"
                :before-upload="beforeAvatarUpload">
                <img v-if="form.fPictureUrl" :src="form.fPictureUrl" class="avatar">
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
              </el-upload>
              </el-form-item>             
            </el-col>
            <el-col :span="12">
              <el-form-item label="备注" prop="fRemark">
                <el-input v-model="form.fRemark" type="textarea" placeholder="请输入内容" />
              </el-form-item>
            </el-col> 
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
    </div>
  </template>
  
  <script>
  import { listBox, getBox, delBox, addBox, updateBox } from "@/api/basicData/box";
  
  export default {
    name: "Box",
    dicts: ['sys_normal_disable'],
    data() {
      return {    
        value: true,  
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 岗位表格数据
        boxList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          postCode: undefined,
          postName: undefined,
          status: undefined
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          postName: [
            { required: true, message: "岗位名称不能为空", trigger: "blur" }
          ],
          postCode: [
            { required: true, message: "岗位编码不能为空", trigger: "blur" }
          ],
          postSort: [
            { required: true, message: "岗位顺序不能为空", trigger: "blur" }
          ],
          fState:[
          { required: true, message: "请选择是否启用不能选空", trigger: "change" }
        ],
        }
      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询岗位列表 */
      getList() {
        this.loading = true;
        const data = [
        {
          postId: '1111',
          postId1: '2222',
          postId2: '温箱台',
          postId3: '555',
          postId4: '工厂',
          postId5: '产线',
          postId6: '设备',
          postId7: '厂家',
          postId8: '品牌',
          postId9: '型号',
          postId10: '96',
          postId11: '52',
          postId12: '正常',
          postId13: '天伦',
          postId14: '2024/2/24',
          postId15: '小明',
          postId16: '2024/2/28'
        },
        {
          postId: '1111',
          postId1: '2222',
          postId2: '温箱台',
          postId3: '555',
          postId4: '工厂',
          postId5: '产线',
          postId6: '设备',
          postId7: '厂家',
          postId8: '品牌',
          postId9: '型号',
          postId10: '96',
          postId11: '52',
          postId12: '正常',
          postId13: '天伦',
          postId14: '2024/2/24',
          postId15: '小明',
          postId16: '2024/2/28'
        }
      ];
      listBox(this.queryParams).then(response => {
        this.boxList = response.rows;
        this.total = response.length;
        this.loading = false;
      });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          postId: undefined,
          postCode: undefined,
          postName: undefined,
          postSort: 0,
          status: "0",
          remark: undefined
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.dateRange = [];
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.fWarnCode)
        this.single = selection.length!=1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加岗位";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const postId = row.fWarnCode || this.ids
        getBox(postId).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改岗位";
        });
      },
      /** 提交按钮 */
      submitForm: function() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.postId != undefined) {
              updateBox(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addBox(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const postIds = row.fWarnCode || this.ids;
        this.$modal.confirm('是否确认删除岗位编号为"' + postIds + '"的数据项？').then(function() {
          return delBox(postIds);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('system/box/export', {
          ...this.queryParams
        }, `post_${new Date().getTime()}.xlsx`)
      }
    }
  };
  </script>
  